home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / Swapping.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.0 KB  |  41 lines

  1. //: C20:Swapping.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // All basic sequence containers can be swapped
  7. #include "Noisy.h"
  8. #include <list>
  9. #include <vector>
  10. #include <deque>
  11. #include <iostream>
  12. #include <algorithm>
  13. using namespace std;
  14. ostream_iterator<Noisy> out(cout, " ");
  15.  
  16. template<class Cont>
  17. void print(Cont& c, char* comment = "") {
  18.   cout << "\n" << comment << ": ";
  19.   copy(c.begin(), c.end(), out);
  20.   cout << endl;
  21. }
  22.  
  23. template<class Cont>
  24. void testSwap(char* cname) {
  25.   Cont c1, c2;
  26.   generate_n(back_inserter(c1), 10, NoisyGen());
  27.   generate_n(back_inserter(c2), 5, NoisyGen());
  28.   cout << "\n" << cname << ":" << endl;
  29.   print(c1, "c1"); print(c2, "c2");
  30.   cout << "\n Swapping the " << cname 
  31.     << ":" << endl;
  32.   c1.swap(c2);
  33.   print(c1, "c1"); print(c2, "c2");
  34. }  
  35.  
  36. int main() {
  37.   testSwap<vector<Noisy> >("vector");
  38.   testSwap<deque<Noisy> >("deque");
  39.   testSwap<list<Noisy> >("list");
  40. } ///:~
  41.